package com.qf.servlet;

import com.qf.domain.Book;
import com.qf.service.BookService;
import com.qf.service.impl.BookServiceImpl;
import com.qf.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName:day13_booksystem
 * author:liuXin
 * time:2022/9/29 9:54
 * description:
 */
@WebServlet("/updateui")
public class UpdateUIServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端数据
        String id = request.getParameter("id");
        System.out.println("id = " + id);
        //非空验证
        if(StringUtils.isEmpty(id)){
            request.setAttribute("msg","书籍id不能为空");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
            return;
        }
        //调用业务逻辑
        try {
            int bookId = Integer.parseInt(id);
            BookService bookService=new BookServiceImpl();
            Book book=bookService.findById(bookId);
            //转发重定向
            if (book != null) {
                request.setAttribute("book",book);
                request.getRequestDispatcher("/update.jsp").forward(request,response);
            }else{
                request.setAttribute("msg","书籍不存在");
                request.getRequestDispatcher("/message.jsp").forward(request,response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","查询书籍失败");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
